import { sm2 } from 'sm-crypto'
import axios from 'axios'
/* eslint-disable */
const decryptSM2 = (text) => {
      let key = '5506c31374d5d8e9d79ba5ecc7cd1a7d60df3f7ef5fbb6865ccd9ce489751ac7'
      const deText = sm2.doDecrypt(text.slice(2), key, 1)
      return deText
}
let requestUrl = '/api-gateway/jpaas-jags-server'
// 判断用户是否登录，最终只返回用户getTokenSNO
const getToken = () => {
      return new Promise((resolve, reject) => {
            // 获取用户信息
            lightAppJssdk.user.getUserInfo({
                  success: data => {
                        if (data == '未登录') {
                              lightAppJssdk.user.loginapp({
                                    success: userResult => {
                                          // 登录后获取数据
                                          if (userResult == '取消登录' || userResult == '用户拒绝授权') {
                                                lightAppJssdk.navigation.close({})
                                          } else {
                                                resolve(JSON.parse(userResult))
                                          }
                                    },
                                    fail: userError => {
                                          reject(userError)
                                    }
                              })
                        } else { // 已经登录
                              if (data == '用户拒绝授权') {
                                    lightAppJssdk.navigation.close({})
                              } else {
                                    resolve(JSON.parse(data))
                              }
                        }
                  },
                  fail: (error) => {
                        reject(error)
                  }
            })
      })
}
// 获取请求头Authorization的值
const getAuthorization = () => {
      const baseParams = new URLSearchParams()
      baseParams.append('app_id', 'tyyhww')
      baseParams.append('version', '1.0')
      baseParams.append('charset', 'UTF-8')
      baseParams.append('origin', '1')
      baseParams.append('timestamp', new Date().valueOf())
      baseParams.append('interface_id', 'getAccessToken')
      baseParams.append('header', JSON.stringify({
            nlkftoken: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJQaXB2cXlWcWNLUzFPTFZvZUtYUlNlVFhrYm9sbzFzMSIsImV4cCI6MTYyNzA0MTMwOSwiaWF0IjoxNTE2MjM5fQ.aCP7GR1lRmMw0uUuTMk80rzG-4a8QQQJ4O_pi1ulheY'
      }))
      baseParams.append('biz_content', JSON.stringify({
            appmark: 'zygkb'
      }))

      return new Promise((resolve, reject) => {
            axios({
                  method: 'post',
                  url: requestUrl + '/interface/createsign.do',
                  data: baseParams
            }).then(res => {
                  baseParams.append('sign', res.data.data.sign)
                  axios({
                        method: 'post',
                        url: requestUrl + '/interface/gateway.do',
                        data: baseParams
                  }).then(data => {
                        resolve(JSON.parse(data.data.data))
                  }).catch(error => {
                        reject(error)
                  })
            }).catch(error => {
                  reject(error)
            })
      })
}

// 获取请个人、法人用户信息
const getUserInfo = async () => {
      const token = await getToken();
      console.log(token, 'token')
      const baseParams = new URLSearchParams()
      baseParams.append('app_id', 'yhkj')
      baseParams.append('version', '1.0')
      baseParams.append('charset', 'UTF-8')
      baseParams.append('origin', '1')
      baseParams.append('timestamp', new Date().valueOf())
      baseParams.append('interface_id', 'getUserInfoByToken')
      baseParams.append('biz_content', JSON.stringify({
            token: token.token
      }))

      return new Promise((resolve, reject) => {
            axios({
                  method: 'post',
                  url: requestUrl + '/interface/createsign.do',
                  data: baseParams
            }).then(res => {
                  baseParams.append('sign', res.data.data.sign)
                  axios({
                        method: 'post',
                        url: requestUrl + '/interface/gateway.do',
                        data: baseParams
                  }).then(data => {
                        let result = JSON.parse(decryptSM2(JSON.parse(data.data.data).data))
                        result.token = token
                        resolve(result)
                  }).catch(error => {
                        reject(error)
                  })
            }).catch(error => {
                  reject(error)
            })
      })
}

export {
      getUserInfo
}
